package net.oschina.bilbodai.common.beanutil.bean.creator;

import net.oschina.bilbodai.common.beanutil.bean.code.IPropertyCodeApplier;
import net.oschina.bilbodai.common.beanutil.bean.code.SimpleToSimpleApplier;
import net.oschina.bilbodai.common.beanutil.bean.core.PropertyDumpInfo;
import net.oschina.bilbodai.common.beanutil.bean.core.PropertyInfo;
import net.oschina.bilbodai.common.beanutil.exception.PropertyDumpException;

/**
 * @author daimao  Date: 16/5/30 Time: 上午11:34
 * @version $Id$
 */
public enum SimpleToSimpleCreator implements IPropertyCodeApplierCreator {
    INSTANCE;

    @Override
    public IPropertyCodeApplier create(PropertyDumpInfo dumpInfo, PropertyInfo sourceInfo, PropertyInfo targetInfo) {
        Class<?> sourceType = sourceInfo.getFieldType();
        Class<?> targetType = targetInfo.getFieldType();
        if (dumpInfo.usingAttrPathQuery()) {
            sourceType = dumpInfo.getAttrPathQueryInfo().getPropertyType();
        }
        if (!sourceType.isAssignableFrom(targetType)) {
            throw new PropertyDumpException(sourceType + " is not assignableFrom " + targetType + ", sourceInfo=" + sourceInfo + ",targetInfo=" + targetInfo);
        }

        return new SimpleToSimpleApplier(dumpInfo);
    }
}